/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelController.sessionController;

import entities.Myidea;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import modelController.viewerController.MainXhtml;
import tools.StaticFields;

/**
 *
 * @author Administrator
 */
@Named("myideaController")
@SessionScoped
public class MyideaController extends CommonModelController<Myidea> implements Serializable {

    private List<Myidea> myideaList = null;
    private final String tableName = "myidea", listpage = "myidea/List", editpage = "myidea/Edit", viewpage = "myidea/View", createpage = "myidea/Create";
    @Inject
    private MainXhtml mainXhtml;
    @Inject
    private modelController.applicationController.MyideaController applicationMyideaController;
    @Inject
    private modelController.applicationController.StudentController applicationStudentController;
    @Inject
    private tools.UserMessagor userMessagor;
    @Inject
    private modelController.sessionController.CommonSession commonSession;
    @Inject
    private modelController.sessionController.KnowledgeController knowledgeController;
    @Inject
    private modelController.sessionController.QuestionController questionController;
    @Inject
    private modelController.applicationController.RoleinfoController applicationRoleinfoController;
    @Inject
    private modelController.applicationController.TeacherAdminController applicationTeacherAdminController;
    @Inject
    private modelController.applicationController.StudentController applicationStudentController1;
    protected Myidea current;

    public Myidea getSelected() {
        if (current == null) {
            selectedItemIndex = -1;
            current = new Myidea();
        }
        return current;
    }

    public void setSelected(Myidea myideas) {
        current = myideas;
    }

    public void setDataModelList() {
        pageOperation.setDataModelList(applicationMyideaController.getMyideaList());
    }

    public MyideaController() {
    }

    public void create(int knowledgeOrQuestion) {
        //0--Knowledge,1--Question
        try {
            switch (knowledgeOrQuestion) {
                case 0:
                    current.setKnowledgeid(knowledgeController.current);
                    break;
                case 1:
                    current.setQuestionid(questionController.current);
                    break;
            }
            if(applicationRoleinfoController.isTeahcer(commonSession.getRoleinfo())){
                current.setTeacherid(applicationTeacherAdminController.find(commonSession.getUser().getId()));
            }
            if(applicationRoleinfoController.isStudent(commonSession.getRoleinfo())){
                current.setStudentid(applicationStudentController1.find(commonSession.getUser().getId()));
            }
            applicationMyideaController.create(current);
            userMessagor.addMessage(commonSession.getResourceBound().getString("Succeed"));
            pageOperation.refreshData(applicationMyideaController.getMyideaList());
            this.logs(current.getId().toString(), tableName, StaticFields.OPERATIONINSERT);
            prepareCreate();
        } catch (Exception e) {
            userMessagor.addMessage(commonSession.getResourceBound().getString("Failed") + "controll myideas 1");
        }
    }

    
    public void prepareCreate() {
        setSelected(new Myidea());
        selectedItemIndex = -1;
        mainXhtml.setPageName(this.createpage);
    }

    public void prepareEdit() {
        setSelected((Myidea) getItems().getRowData());
        selectedItemIndex = getPageOperation().getPagination().getPageFirstItem() + getItems().getRowIndex();
        mainXhtml.setPageName(editpage);
    }

    public String update() {
        try {
            applicationMyideaController.edit(current);
            //evictForeignKey();
            userMessagor.addMessage(commonSession.getResourceBound().getString("Succeed"));
            this.logs(current.getId().toString(), tableName, StaticFields.OPERATIONUPDATE);
            return null;
        } catch (Exception e) {
            userMessagor.addMessage(commonSession.getResourceBound().getString("Failed") + "controll myideas 2");
            return null;
        }
    }

    public void destroy() {
        current = (Myidea) getItems().getRowData();
        selectedItemIndex = pageOperation.getPagination().getPageFirstItem() + getItems().getRowIndex();
        performDestroy();
        setSelected(null);
        mainXhtml.setPageName(this.listpage);
    }

    private void performDestroy() {
        try {
            applicationMyideaController.remove(current);
            //evictForeignKey();
            userMessagor.addMessage(commonSession.getResourceBound().getString("Succeed"));
            updateCurrentItem();
            pageOperation.refreshData(applicationMyideaController.getMyideaList());
            this.logs(current.getId().toString(), tableName, StaticFields.OPERATIONDELETE);
        } catch (Exception e) {
            userMessagor.addMessage(commonSession.getResourceBound().getString("Failed") + "controll myideas 3");
        }
    }

    private void updateCurrentItem() {
        int count = applicationMyideaController.count();
        if (selectedItemIndex >= count) {
            // selected index cannot be bigger than number of items:
            selectedItemIndex = count - 1;
            // go to previous page if last page disappeared:
            if (getPageOperation().getPagination().getPageFirstItem() >= count) {
                getPageOperation().getPagination().previousPage();
            }
        }
        if (selectedItemIndex >= 0) {
            setSelected((Myidea) applicationMyideaController.findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0));
        }
    }
}
